package com.framework.buss.bridge.controller;

import com.framework.buss.bridge.model.BridgeTemplate;
import com.framework.buss.bridge.service.IBridgeTemplateService;
import com.framework.buss.bridge.vo.BridgeTemplateVO;
import com.framework.common.domain.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags="桥梁配置表")
@RestController
@RequestMapping("/template")
@Slf4j
public class BridgeTemplateController {
    @Autowired
    private IBridgeTemplateService bridgeTemplateService;

    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/get/{id}")
    public ResultData<BridgeTemplate> get(@PathVariable(name="id") String id) {
        BridgeTemplate template = bridgeTemplateService.getById(id);
        if(template==null) {
            return ResultData.error("未找到对应数据");
        }
        return ResultData.data(template);
    }

    /**
     * @param bridgeTemplate
     * @return
     */
    //@AutoLog(value = "桥梁表-分页列表查询")
    @ApiOperation(value="桥梁表-列表查询", notes="桥梁表-列表查询")
    @GetMapping(value = "/list")
    public ResultData<List<BridgeTemplateVO>> listAll(BridgeTemplate bridgeTemplate) {
        List<BridgeTemplateVO> pageList = bridgeTemplateService.listAll(bridgeTemplate);
        return ResultData.data(pageList);
    }

    /**
     *   添加
     *
     * @param bridgeTemplate
     * @return
     */
    @ApiOperation(value="桥梁表-添加", notes="桥梁表-添加")
    @PostMapping(value = "/add")
    public ResultData<String> add(@RequestBody BridgeTemplate bridgeTemplate) {
        bridgeTemplateService.save(bridgeTemplate);
        return ResultData.success("添加成功！");
    }

    @ApiOperation(value="桥梁表-添加", notes="桥梁表-添加")
    //@RequiresPermissions("org.jeecg.biz.bridge:_bridge:add")
    @PostMapping(value = "/addAll")
    public ResultData<String> addAll(@RequestBody List<BridgeTemplateVO> list,
                                 @RequestParam(name="bridgeId") String bridgeId,
                                 @RequestParam(name="filePath") String filePath) {
        bridgeTemplateService.saveAll(list, bridgeId, filePath);
        return ResultData.success("添加成功！");
    }

    /**
     *  编辑
     *
     * @param bridgeTemplate
     * @return
     */
    @ApiOperation(value="桥梁表-编辑", notes="桥梁表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public ResultData<String> edit(@RequestBody BridgeTemplate bridgeTemplate) {
        bridgeTemplateService.updateById(bridgeTemplate);
        return ResultData.success("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="桥梁表-通过id删除", notes="桥梁表-通过id删除")
    @DeleteMapping(value = "/delete")
    public ResultData<String> delete(@RequestParam(name="id",required=true) String id) {
        bridgeTemplateService.removeById(id);
        return ResultData.success("删除成功!");
    }

    /**
     * 通过excel导入配置文件
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importSetting", method = RequestMethod.POST)
    public ResultData<?> importSetting(HttpServletRequest request, HttpServletResponse response) {
        return bridgeTemplateService.importSetting(request, response);
    }
}
